package site.anxuan.seckill.service.impl;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import site.anxuan.seckill.common.BusinessException;
import site.anxuan.seckill.common.ErrorCode;
import site.anxuan.seckill.dao.ItemStockLogDao;
import site.anxuan.seckill.entity.ItemStockLog;
import site.anxuan.seckill.service.ItemStockLogService;

import java.util.UUID;

@Service
@AllArgsConstructor
public class ItemStockLogServiceImpl implements ItemStockLogService {

    private final ItemStockLogDao itemStockLogDao;

    @Override
    public ItemStockLog insert(int itemId, int amount) {
        if (itemId < 0 || itemId <= amount) {
            throw new BusinessException(ErrorCode.PARAMETER_ERROR, "参数不合法！");
        }
        ItemStockLog itemStockLog = new ItemStockLog();
        itemStockLog.setId(UUID.randomUUID().toString())
                .setItemId(itemId)
                .setAmount(amount)
                .setStatus(ItemStockLog.State.DEFAULT);
        itemStockLogDao.insert(itemStockLog.getId(), itemId, amount, itemStockLog.getStatus());
        return itemStockLog;
    }

    @Override
    public boolean updateStatus(String itemStockLogId, int status) {
        int num = itemStockLogDao.updateStatus(itemStockLogId, status);
        return num > 0;
    }

    @Override
    public ItemStockLog getById(String id) {
        return itemStockLogDao.getById(id);
    }

}
